use tauri::{
    menu::{MenuBuilder, MenuItemBuilder},
    App, Manager,
};

use crate::types::TResult;

pub fn init(app: &mut App) -> TResult<()> {
    let setting = MenuItemBuilder::with_id("setting", "打开设置").build(app)?;
    let quit = MenuItemBuilder::with_id("quit", "退出").build(app)?;
    let menu = MenuBuilder::new(app).items(&[&setting, &quit]).build()?;
    let icon = tauri::image::Image::from_bytes(include_bytes!("../icons/icon.png"))?;
    tauri::tray::TrayIconBuilder::new()
        .menu(&menu)
        .icon(icon)
        .on_menu_event(move |app, event| match event.id().as_ref() {
            "setting" => {
                if let Some(webview_window) = app.get_webview_window("main") {
                    let _ = webview_window.show();
                    let _ = webview_window.set_focus();
                }
            }
            "quit" => {
                std::process::exit(0);
            }
            _ => (),
        })
        .build(app)?;
    Ok(())
}
